package com.iterator.service.wechat.wy.collect;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.wechat.wy.collect.AddordeletevVO;
import com.iterator.bean.wechat.wy.collect.CollectListBean;
import com.iterator.bean.wechat.wy.collect.CollectVO;
import com.iterator.mapper.wechat.wy.collect.WxCollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Silence_W
 * @date: 2021-01-12 21:05
 **/
@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    WxCollectMapper wxCollectMapper;

    @Override
    public CollectVO queryCollect(Integer id, Integer type, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<CollectListBean> collectListBeans= wxCollectMapper.queryCollectById(id);

        PageInfo<CollectListBean> collectListBeanPageInfo = new PageInfo<>(collectListBeans);
        int total = (int) collectListBeanPageInfo.getTotal();

        return new CollectVO(total,collectListBeans);
    }

    @Override
    public AddordeletevVO deleteCollect(Integer id, Byte type, Integer valueId) {
        int nums = wxCollectMapper.queryCollectByIdAndValueId(id,valueId);
        if (nums>0){

            wxCollectMapper.deleteCollect(id,valueId);
            return new AddordeletevVO("delete");
        }else{
            wxCollectMapper.insertCollect(id,valueId,type,new Date(),new Date(),0);
            return  new AddordeletevVO("add");
        }


    }

    @Override
    public Integer queryIdByUsername(String username) {
        Integer id = wxCollectMapper.queryIdByUsername(username);
        return id;
    }


}
